package com.haiyou.common.thread.task;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Stopwatch;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 
 * @Description: Callable 线程
 * @author xingyuan
 * @date 2022年7月6日 下午4:31:26
 */
@Slf4j
public abstract class AbstractCallableCoreTask<V> extends AbstractCoreTask implements Callable<V>{

	@Override
	public V call() throws Exception {
		Stopwatch stopwatch = Stopwatch.createStarted();
		try {
			return exec();
		} catch (Exception e) {
			log.error(ex.getMessage(), ex);// 任务提交堆栈处
			log.error(e.getMessage(), e);// 异常出现堆栈处
			return null;
		} finally {
			long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
			if (elapsed > executeSlowly())
				log.warn("SLOWLY: {}, used {} ms", this, elapsed);
			else
				log.debug("{}, used {} ms", this, elapsed);
		}
	}
	
	public abstract V exec() throws Exception;
}
